package cn.edu.xmu.restfuldemo.service;

import cn.edu.xmu.restfuldemo.dao.OrderItemDao;
import cn.edu.xmu.restfuldemo.dao.OrdersDao;
import cn.edu.xmu.restfuldemo.model.BO.OrderItem;
import cn.edu.xmu.restfuldemo.model.BO.Orders;
import cn.edu.xmu.restfuldemo.model.PO.OrderItemPo;
import cn.edu.xmu.restfuldemo.model.VO.OrdersRetVo;
import cn.edu.xmu.restfuldemo.model.VO.OrdersVo;
import cn.edu.xmu.restfuldemo.util.ResponseCode;
import cn.edu.xmu.restfuldemo.util.ReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Team3-5
 */
@Service
public class OrdersService {
    @Autowired
    OrdersDao ordersDao;

    @Autowired
    OrderItemDao orderItemDao;

    /**
     * 插入orders
     * @param ordersRetVo ordersRetVo
     * @return returnObject
     */
    public ReturnObject addOrders(OrdersRetVo ordersRetVo){
        ReturnObject<Long>returnObject = ordersDao.addOrder(ordersRetVo.newOrdersFromOrdersRetVo());
        Long orderId = returnObject.getData();
        for(OrderItem orderItem:ordersRetVo.newOrderItemList()){
            orderItem.setOrderId(orderId);
            orderItemDao.addOrderItem(orderItem);
        }
        return new ReturnObject(ResponseCode.OK,"成功");
    }

    /**
     * 根据id获得ordersRetVo
     * @param id id
     * @return returnObject
     */
    public ReturnObject<OrdersRetVo> findOrders(Long id){
        ReturnObject<Orders> returnObjectAboutOrders = ordersDao.findOrdersById(id);
        ReturnObject<List<OrderItem>> returnObjectAboutOrderItemList = orderItemDao.findOrderItemListByOrderId(id);
        return new ReturnObject<OrdersRetVo>(new OrdersRetVo((Orders)returnObjectAboutOrders.getData(),(List<OrderItem>)returnObjectAboutOrderItemList.getData()));
    }

    /**
     * 根据id获得ordersRetVo
     * @param id id
     * @return returnObject
     */
    public ReturnObject<OrdersRetVo> findOrdersRetVo(Long id){
        return ordersDao.findOrdersByOrdersId(id);
    }
}
